// Vue
import Vue from 'vue'
import App from './App'
// 核心插件
import d2Admin from '@/plugin/d2admin'
// store
import store from '@/store/index'
// 多国语
import i18n from './i18n'
// 菜单和路由设置
import router from './router'
//import menuHeader from '@/menu/header'
//import menuAside from '@/menu/aside'
import {
	mHeader,
	mAside
} from '@api/sys.menu'
import {
	frameInRoutes
} from '@/router/routes'
import echarts from 'echarts'
//表格
import d2Crud from '@d2-projects/d2-crud'
// [ 可选插件组件 ] UEditor
import VueUeditorWrap from 'vue-ueditor-wrap'
import util from '@/libs/util.js'

// 核心插件
Vue.use(d2Admin, {
	store
})
Vue.use(d2Crud)
Vue.prototype.$echarts = echarts;
Vue.component('VueUeditorWrap', VueUeditorWrap)

import '@/assets/style/common.scss' //初始化、公共样式

new Vue({
	router,
	store,
	i18n,
	render: h => h(App),
	created() {
		// 处理路由 得到每一级的路由设置
		this.$store.commit('d2admin/page/init', frameInRoutes)
		// 设置顶栏菜单
		this.$store.commit('d2admin/menu/headerSet', mHeader)
		// 设置侧边栏菜单
		//this.$store.commit('d2admin/menu/asideSet', menuAside)
		// 初始化菜单搜索功能
		this.$store.commit('d2admin/search/init', mHeader)
	},
	mounted() {
		// 展示系统信息
		this.$store.commit('d2admin/releases/versionShow')
		// 用户登录后从数据库加载一系列的设置
		const token = util.cookies.get('token')
		if (token && token !== 'undefined' && token !== 'null') {
			this.$store.dispatch('d2admin/account/load')
		}
		// 获取并记录用户 UA
		this.$store.commit('d2admin/ua/get')
		// 初始化全屏监听
		this.$store.dispatch('d2admin/fullscreen/listen')
	},
	watch: {
		// 检测路由变化切换侧边栏内容
		'$route.matched': {
			handler(matched) {
				let preferences = parseInt(util.cookies.get('preferences')) //左侧菜单显示偏好 1沉浸式 0普通
				if (matched.length > 0 && preferences > 0) {
					const _side = mAside.filter(menu => menu.path === matched[0].path)
					this.$store.commit('d2admin/menu/asideSet', _side.length > 0 ? _side[0].children : [])
				}
			},
			immediate: true
		}
	}
}).$mount('#app')
